package ithink.link.bean;

import com.alibaba.fastjson.JSONObject;
import ithink.link.exception.ParameterErrorException;
import ithink.link.stack.IConnection;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * Created by zaqb on 17-10-26.
 */
public class DeviceInfo {
    private String sid = "";
    private String extIp = "";
    private String innerIp = "";
    private String udpPort = "";
    private String tcpPort = "";
    private String udpState = "";
    private String tcpState = "";
    private String isPortMapping = "";
    private Date loginDate;
    private IConnection connection;
    private String serverName = "";

    public void update(IConnection connection, JSONObject json) throws ParameterErrorException {
        if(connection == null || json == null) {
            throw new ParameterErrorException("connection or json is null");
        }
        this.extIp = connection.remoteIp();
        this.sid = json.getString("sid");
        this.innerIp = json.getString("dev-innerip");
        this.udpPort = json.getString("dev-port-udp");
        this.tcpPort = json.getString("dev-port-tcp");
        this.udpState = json.getString("dev-udpstate");
        this.tcpState = json.getString("dev-tcpstate");
        this.isPortMapping = json.getString("dev-isportmap");
        this.loginDate = new Date();
        this.connection = connection;
        if(StringUtils.isBlank( extIp ) || StringUtils.isBlank( innerIp )
                || StringUtils.isBlank( udpPort ) || StringUtils.isBlank( tcpPort )
                || StringUtils.isBlank( udpState ) || StringUtils.isBlank( tcpState )
                || StringUtils.isBlank( isPortMapping )) {
            throw new ParameterErrorException(json.toJSONString());
        }
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getExtIp() {
        return extIp;
    }

    public void setExtIp(String extIp) {
        this.extIp = extIp;
    }

    public String getInnerIp() {
        return innerIp;
    }

    public void setInnerIp(String innerIp) {
        this.innerIp = innerIp;
    }

    public String getUdpPort() {
        return udpPort;
    }

    public void setUdpPort(String udpPort) {
        this.udpPort = udpPort;
    }

    public String getTcpPort() {
        return tcpPort;
    }

    public void setTcpPort(String tcpPort) {
        this.tcpPort = tcpPort;
    }

    public String getUdpState() {
        return udpState;
    }

    public void setUdpState(String udpState) {
        this.udpState = udpState;
    }

    public String getTcpState() {
        return tcpState;
    }

    public void setTcpState(String tcpState) {
        this.tcpState = tcpState;
    }

    public String getIsPortMapping() {
        return isPortMapping;
    }

    public void setIsPortMapping(String isPortMapping) {
        this.isPortMapping = isPortMapping;
    }

    public Date getLoginDate() {
        return loginDate;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    public IConnection getConnection() {
        return connection;
    }

    public void setConnection(IConnection connection) {
        this.connection = connection;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }
}
